package v1

import (
	"fmt"
	"gitee.com/xuaxu/go-gin/pkg/app"
	"gitee.com/xuaxu/go-gin/pkg/e"
	"gitee.com/xuaxu/go-gin/pkg/util"
	"gitee.com/xuaxu/go-gin/service/user"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type Auth struct {
	ApaasId int64
}

func Login(c *gin.Context) {
	appG := app.Gin{C: c}
	assId := c.PostForm("asst_id")
	//获取用户id
	id := int64(0)
	id, _, _ = user.Getuser(assId)

	iStrue, err, asstId := user.Login(id)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_TAG_FAIL, nil)
		return
	}
	//生产加密token
	token, err := util.GenerateToken(id)
	appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
		"asstId": asstId,
		"isTrue": iStrue,
		"token":  token,
	})
}

func GetUser(c *gin.Context) {
	appG := app.Gin{C: c}
	assId := c.PostForm("asst_id")
	fmt.Println(assId)
	id, err := strconv.ParseInt(assId, 10, 64)

	iStrue, err, asstId := user.Login(id)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_TAG_FAIL, nil)
		return
	}
	//生产加密token
	token, err := util.GenerateToken(id)
	appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
		"asstId": asstId,
		"isTrue": iStrue,
		"token":  token,
	})
}
